package com.after.saas.user.controller;


import com.after.saas.user.model.DictData;
import com.after.saas.user.service.DictDataService;
import com.after.saas.common.constants.Result;
import com.after.saas.common.model.bo.CoreDictBO;
import com.after.saas.common.model.query.CommonQuery;
import com.after.saas.common.utils.EasyExcelUtil;
import com.after.saas.common.utils.ListUtil;
import com.after.saas.common.utils.ResponseUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author hjchen
 * @since 2021-07-18
 */

@RestController
@Api(tags = "字典数据管理")
@RequestMapping("/dict-data")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    @PostMapping("/batchSave")
    private Result<Boolean> batchSave(@RequestBody List<CoreDictBO> dictBOS) {
        return ResponseUtil.SUCCESS(dictDataService.saveBatch(ListUtil.copyList(dictBOS, DictData.class)));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, CommonQuery query) {
        List<CoreDictBO> dictBOS = ListUtil.copyList(dictDataService.list(), CoreDictBO.class);
        EasyExcelUtil.export(response, dictBOS, CoreDictBO.class, "测试导出字典数据");
    }

    @GetMapping("/listPage")
    public Result<PageInfo<DictData>> listPage(CommonQuery query) {
        return ResponseUtil.SUCCESS(dictDataService.listPage(query));
    }

    @GetMapping("/page")
    public Result<Page<DictData>> page(CommonQuery query) {
        return ResponseUtil.SUCCESS(dictDataService.page(new Page<>(query.getPage(), query.getSize())));
    }

}
